﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data.Objects;

namespace CustomerService
{
    public partial class EditCustomer : System.Web.UI.Page
    {
        private SalesManagerEntities _sme;
        private tblCustomer _customer = null;
        private int _customerID;

        protected void Page_Load(object sender, EventArgs e)
        {
            _sme = (SalesManagerEntities)Session["sme"];
            int.TryParse(Request.QueryString["CustomerID"], out _customerID);
            if (!IsPostBack)
            {
                SqlConnection conn = (SqlConnection)((System.Data.EntityClient.EntityConnection)_sme.Connection).StoreConnection;
                if (conn.State != System.Data.ConnectionState.Open )
                    conn.Open();
                SqlCommand cmm = conn.CreateCommand();

                cmm.CommandText = "exec spLoadCustomer @CustomerID";
                cmm.Parameters.Add(new SqlParameter("@CustomerID", _customerID));
                SqlDataReader rdr = cmm.ExecuteReader();

                _sme.Translate<codeCountry>(rdr, "SalesManagerEntities.codeCountry", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                _sme.Translate<tblAddress>(rdr, "SalesManagerEntities.tblAddress", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                _sme.Translate<tblPerson>(rdr, "SalesManagerEntities.tblPerson", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                _sme.Translate<codeContactPointType>(rdr, "SalesManagerEntities.codeContactPointType", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                _customer = _sme.Translate<tblCustomer>(rdr, "SalesManagerEntities.tblCustomer", MergeOption.AppendOnly).FirstOrDefault();
                rdr.NextResult();
                _customer.cachedContactPoints = _sme.Translate<tblContactPoint>(rdr, "SalesManagerEntities.tblContactPoint", MergeOption.AppendOnly).ToArray();
                rdr.NextResult();
                _sme.Translate<codeHoldReason>(rdr, "SalesManagerEntities.codeHoldReason", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                _sme.Translate<codeOrderSource>(rdr, "SalesManagerEntities.codeOrderSource", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                _sme.Translate<tblPromotion>(rdr, "SalesManagerEntities.tblPromotion", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                _sme.Translate<codeOrderType>(rdr, "SalesManagerEntities.codeOrderType", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                _sme.Translate<codeOrderStatus>(rdr, "SalesManagerEntities.codeOrderStatus", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                _customer.cachedOrderHeaders = _sme.Translate<tblOrderHeader>(rdr, "SalesManagerEntities.tblOrderHeader", MergeOption.AppendOnly).ToArray();
                rdr.NextResult();
                _sme.Translate<tblProduct>(rdr, "SalesManagerEntities.tblProduct", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                _customer.cachedOrderDetails = _sme.Translate<tblOrderDetail>(rdr, "SalesManagerEntities.tblOrderDetail", MergeOption.AppendOnly).ToArray();
                rdr.NextResult();
                _sme.Translate<tblSequence>(rdr, "SalesManagerEntities.tblSequence", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                _customer.cachedSequenceOrders = _sme.Translate<tblSequenceOrder>(rdr, "SalesManagerEntities.tblSequenceOrder", MergeOption.AppendOnly).ToArray();
                rdr.NextResult();
                _customer.cachedSequenceSteps = _sme.Translate<tblSequenceStep>(rdr, "SalesManagerEntities.tblSequenceStep", MergeOption.AppendOnly).ToArray();
                rdr.NextResult();
                _customer.cachedRefunds = _sme.Translate<tblRefund>(rdr, "SalesManagerEntities.tblRefund", MergeOption.AppendOnly).ToArray();
                rdr.NextResult();
                _customer.cachedRMAReturns = _sme.Translate<tblRMAReturn>(rdr, "SalesManagerEntities.tblRMAReturn", MergeOption.AppendOnly).ToArray();
                rdr.NextResult();
                _customer.cachedReturnDetails = _sme.Translate<tblReturnDetail>(rdr, "SalesManagerEntities.tblReturnDetail", MergeOption.AppendOnly).ToArray();
                rdr.NextResult();
                _sme.Translate<codeTransactionStatus>(rdr, "SalesManagerEntities.codeTransactionStatus", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                _sme.Translate<codeTransactionType>(rdr, "SalesManagerEntities.codeTransactionType", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                _customer.cachedTransactions = _sme.Translate<tblTransaction>(rdr, "SalesManagerEntities.tblTransaction", MergeOption.AppendOnly).ToArray();
                rdr.NextResult();
                _sme.Translate<tblAccount>(rdr, "SalesManagerEntities.tblAccount", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                _sme.Translate<tblTransactionDetail>(rdr, "SalesManagerEntities.tblTransactionDetail", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                _customer.cachedMiscDefs = _sme.Translate<tblMiscDef>(rdr, "SalesManagerEntities.tblMiscDef", MergeOption.AppendOnly).ToArray();
                // Load the selcted customer from the database
                //IQueryable<tblCustomer> oq =
                //    from c in _sme.tblCustomer
                //        .Include("Person")
                //        .Include("Person.Address")
                //        .Include("Person.Address.Country")
                //        .Include("Person.Language")
                //        .Include("Person.ContactPoints")
                //        .Include("Person.ContactPoints.ContactPointType")
                //        .Include("OrderHeaders")
                //        .Include("OrderHeaders.ShipTo_Person")
                //        .Include("OrderHeaders.ShipTo_Address")
                //        .Include("OrderHeaders.ShipTo_Address.Country")
                //        .Include("OrderHeaders.HoldReason")
                //        .Include("OrderHeaders.OrderSource")
                //        .Include("OrderHeaders.OrderType")
                //        .Include("OrderHeaders.OrderStatus")
                //        .Include("OrderHeaders.OrderDetails")
                //        .Include("OrderHeaders.OrderDetails.Product")
                //        .Include("OrderHeaders.SequenceOrders.SequenceSteps.Sequence")
                //    where c.CustomerID == _customerID
                //    select c;
                ////string t = ((System.Data.Objects.ObjectQuery)oq).ToTraceString();
                //_customer = oq.First(f => f.CustomerID == _customerID);
                Session["OrderIndex"] = 0;
            }
            else
                _customer = ((tblCustomer)_sme.GetObjectByKey(new System.Data.EntityKey("SalesManagerEntities.tblCustomer", "CustomerID", _customerID)));

            Load();
        }

        protected new void Load()
        {
            // Load the form from the database
            ((CustomerTab)TabContainer1.FindControl("TabPanelCustomer").FindControl("customerTab1")).Load(_sme, _customer); 
            ((OrderTab)TabContainer1.FindControl("TabPanelOrders").FindControl("orderTab1")).Load(_sme, _customer);
            ((SequenceTab)TabContainer1.FindControl("TabPanelSequences").FindControl("sequenceTab1")).Load(_sme, _customer);
            ((TransactionsTab)TabContainer1.FindControl("TabPanelTransactions").FindControl("transactionsTab1")).Load(_sme, _customer.cachedTransactions);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Page.Validate();
            if (Page.IsValid)
            {
                _sme.SaveChanges();
            }
        }

        protected void btnHome_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx");
        }
    }
}
